package com.oa.ylms.apps.actions.leave;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.oa.ylms.apps.actions.BasicAction;
import com.oa.ylms.apps.business.leave.ILeaveCheckService;
import com.oa.ylms.apps.business.leave.ILeaveInfoService;
import com.oa.ylms.apps.constants.leave.ILeaveInfoStates;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.leave.LeaveCheckValue;
import com.oa.ylms.apps.entity.leave.LeaveInfoValue;

@SuppressWarnings("serial")
public class LeaveInfoAction extends BasicAction {

	private ILeaveInfoService leaveInfoService;
	private ILeaveCheckService leaveCheckService;
	private List<LeaveInfoValue> list;
	private LeaveInfoValue leaveInfo;
	private List<LeaveCheckValue> leaveCheckList;
	private Integer sectionId;
	private Integer categoryId;
	private Integer superSectionId;
	private Integer staffId;
	private String staffName;
	private String title;
	private Integer leaveId;
	private Integer delete_id;
	private String tipInformation;
	private String leaveIds;
	private Integer states = ILeaveInfoStates.UNDEFINED;
	private Map<String,Object> leaveCheckListMap;

	public String add() {
		staffInfo = (StaffInfoValue) session.get("login_staffInfo");
		try {
			leaveInfoService.addLeaveInfo(staffInfo, leaveInfo.getTitle(),
					leaveInfo.getReason(), leaveInfo.getStartTime(), leaveInfo
							.getEndTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "add";
	}

	public String delete() {
		try {
			leaveInfoService.deleteLeaveInfo(delete_id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "delete";
	}

	public String deleteMore() {
		try {
			leaveInfoService.deleteLeaveInfo(leaveIds);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "delete";
	}

	public String queryDetail() {
		try {
			leaveCheckList = leaveCheckService.queryLeaveCheckByLeaveId(leaveId);			                                         
		} catch (Exception e) {
			e.printStackTrace();
		}
		Gson gson = new Gson();
		leaveCheckListMap.put("leaveCheckList", leaveCheckList);
		String str = gson.toJson(leaveCheckListMap);
		try {
			response.setContentType("text/html; charset=UTF-8");
			response.setHeader("Cache-Control", "no-cache");
			response.getWriter().println(str);

		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String manager() {
		staffInfo = (StaffInfoValue) session.get("login_staffInfo");
		try {
			totalpage = leaveInfoService.countTpagesByStaffId(staffInfo, rows);
			count = leaveInfoService.getTotalNumber();
			if (count % rows == 0 && totalpage + 1 == currpage) {
				currpage = currpage - 1;
				totalpage = currpage;
			}
			list = leaveInfoService.queryLeaveInfoByStaffIdWithPage(staffInfo,
					rows, currpage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "manager";
	}

	public String queryByChairman() {
		try {
			totalpage = leaveInfoService.countTpagesByChairman(staffName,
					title, rows);
			count = leaveInfoService.getTotalNumber();
			if (count % rows == 0 && totalpage + 1 == currpage) {
				currpage = currpage - 1;
				totalpage = currpage;
			}
			list = leaveInfoService.queryLeaveInfoByChairmainWithPage(
					staffName, title, rows, currpage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "showByChairman";
	}

	public String queryByMinister() {
		staffInfo = (StaffInfoValue) session.get("login_staffInfo");
		int sectionId = staffInfo.getArchiveInfo().getPostInfo()
				.getSectionInfo().getSectionId();
		try {
			totalpage = leaveInfoService.countTpagesByMinister(sectionId,
					staffName, title, rows);
			count = leaveInfoService.getTotalNumber();
			if (count % rows == 0 && totalpage + 1 == currpage) {
				currpage = currpage - 1;
				totalpage = currpage;
			}
			list = leaveInfoService.queryLeaveInfoByMinisterWithPage(sectionId,
					staffName, title, rows, currpage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "showByMinister";
	}

	public String queryByStaffId() {
		staffInfo = (StaffInfoValue) session.get("login_staffInfo");
		int staffId = staffInfo.getStaffId();
		try {
			totalpage = leaveInfoService.countTpagesByStaffId(staffId, states,
					rows);
			count = leaveInfoService.getTotalNumber();
			if (count % rows == 0 && totalpage + 1 == currpage) {
				currpage = currpage - 1;
				totalpage = currpage;
			}
			list = leaveInfoService.queryLeaveInfoByStaffIdWithPage(staffId,
					states, rows, currpage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "show";
	}

	public ILeaveInfoService getLeaveInfoService() {
		return leaveInfoService;
	}

	public void setLeaveInfoService(ILeaveInfoService leaveInfoService) {
		this.leaveInfoService = leaveInfoService;
	}

	public ILeaveCheckService getLeaveCheckService() {
		return leaveCheckService;
	}

	public void setLeaveCheckService(ILeaveCheckService leaveCheckService) {
		this.leaveCheckService = leaveCheckService;
	}

	public List<LeaveInfoValue> getList() {
		return list;
	}

	public void setList(List<LeaveInfoValue> list) {
		this.list = list;
	}

	public LeaveInfoValue getLeaveInfo() {
		return leaveInfo;
	}

	public void setLeaveInfo(LeaveInfoValue leaveInfo) {
		this.leaveInfo = leaveInfo;
	}

	public Integer getSectionId() {
		return sectionId;
	}

	public void setSectionId(Integer sectionId) {
		this.sectionId = sectionId;
	}

	public Integer getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}

	public Integer getSuperSectionId() {
		return superSectionId;
	}

	public void setSuperSectionId(Integer superSectionId) {
		this.superSectionId = superSectionId;
	}

	public Integer getStaffId() {
		return staffId;
	}

	public void setStaffId(Integer staffId) {
		this.staffId = staffId;
	}

	public String getStaffName() {
		return staffName;
	}

	public void setStaffName(String staffName) {
		this.staffName = staffName;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Integer getLeaveId() {
		return leaveId;
	}

	public void setLeaveId(Integer leaveId) {
		this.leaveId = leaveId;
	}

	public List<LeaveCheckValue> getLeaveCheckList() {
		return leaveCheckList;
	}

	public void setLeaveCheckList(List<LeaveCheckValue> leaveCheckList) {
		this.leaveCheckList = leaveCheckList;
	}

	public Integer getDelete_id() {
		return delete_id;
	}

	public void setDelete_id(Integer delete_id) {
		this.delete_id = delete_id;
	}

	public String getTipInformation() {
		return tipInformation;
	}

	public void setTipInformation(String tipInformation) {
		this.tipInformation = tipInformation;
	}

	public String getLeaveIds() {
		return leaveIds;
	}

	public void setLeaveIds(String leaveIds) {
		this.leaveIds = leaveIds;
	}

	public Integer getStates() {
		return states;
	}

	public void setStates(Integer states) {
		this.states = states;
	}

	public Map<String, Object> getLeaveCheckListMap() {
		return leaveCheckListMap;
	}

	public void setLeaveCheckListMap(Map<String, Object> leaveCheckListMap) {
		this.leaveCheckListMap = leaveCheckListMap;
	}


}
